package ihm;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import dao.DaoCollaborateurMySql;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JSpinner;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import metier.Collaborateur;
import metier.Tache;
import metier.Contrat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Window.Type;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import java.util.ArrayList;

public class ajoutCollab extends JFrame {

	private JPanel contentPane;
	private JTextField txtPrenom;
	private JTextField txtNom;
	private JTextField txtMatricule;
	private JButton btnAccepter;
	private JButton btnAnnuler;
	private JDialog monAjoutCollab;
	//private DaoCollaborateurMySql dao;
	private ArrayList<Collaborateur> listCollab;
	private Maquette maMaquette;

	/**
	 * Create the frame.
	 */
	public ajoutCollab() {
		
		setType(Type.UTILITY);
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setAlwaysOnTop(true);
		setResizable(false);
		setTitle("Ajouter un collaborateur");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 291, 177);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		//dao = new DaoCollaborateurMySql();
		maMaquette = new Maquette();
		
		
		txtPrenom = new JTextField();
		txtPrenom.setColumns(10);
		txtPrenom.setBounds(157, 61, 107, 20);
		contentPane.add(txtPrenom);
		
		txtNom = new JTextField();
		txtNom.setColumns(10);
		txtNom.setBounds(157, 36, 107, 20);
		contentPane.add(txtNom);
		
		JLabel lbMatricule = new JLabel("Matricule collaborateur :");
		lbMatricule.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lbMatricule.setBounds(12, 13, 135, 14);
		contentPane.add(lbMatricule);
		
		JLabel lbNom = new JLabel("Nom");
		lbNom.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lbNom.setAlignmentX(1.0f);
		lbNom.setBounds(12, 38, 71, 14);
		contentPane.add(lbNom);
		
		JLabel lbPrenom = new JLabel("Pr\u00E9nom");
		lbPrenom.setFont(new Font("Segoe UI", Font.PLAIN, 11));
		lbPrenom.setBounds(10, 63, 107, 14);
		contentPane.add(lbPrenom);
		/*
		 * Construction du bouton ajouter
		 */
		btnAccepter = new JButton("Accepter");
		btnAccepter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				boolean valider = true;
				if(txtNom.getText().length() <= 0 || txtPrenom.getText().length() <= 0) {
					valider = false;
				}
				
					if(valider){
						Collaborateur monAjout = new Collaborateur(Integer.parseInt(txtMatricule.getText()), txtNom.getText(), txtPrenom.getText());
						maMaquette.dao.create(monAjout);
						setVisible(false);
						afficherMaquette();
						maMaquette.chargerListeCollab();
						
					}
					else
					{
						JOptionPane.showMessageDialog(null,"Erreur, champs non remplis ! /!\\");
					}
			}
		});
		
		btnAccepter.setBounds(28, 105, 89, 23);
		contentPane.add(btnAccepter);
		/*
		 * Fermeture par le bouton annuler
		 */
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addMouseListener(new MouseAdapter() {
			@Override   
			public void mouseClicked(MouseEvent e) {
				int res = JOptionPane.showConfirmDialog(null, "Annuler l'ajout ?", "Annuler ?", JOptionPane.OK_CANCEL_OPTION);
				if (res == JOptionPane.OK_OPTION) {
					setVisible(false);
					afficherMaquette();
					
				}
			}
		});
		btnAnnuler.setBounds(127, 105, 89, 23);
		contentPane.add(btnAnnuler);
		/*
		 * Fermeture par la croix
		 */
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				afficherMaquette();
			}
		});
		txtMatricule = new JTextField();
		txtMatricule.setEnabled(false);
		txtMatricule.setColumns(10);
		txtMatricule.setBounds(157, 11, 28, 20);
		contentPane.add(txtMatricule);
		int lastMatricule = maMaquette.dao.getLastCollab();
		txtMatricule.setText(Integer.toString(lastMatricule + 1));
	}
	
	public void afficherMaquette(){
		runMaquette.maMaquette.setEnabled(true);
		runMaquette.maMaquette.toFront();
	}
}
